/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socafeclient;

import entity.Comanda;
import entity.Comandaproducto;
import entity.Productos;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author alberto
 */
public class ProductosView extends javax.swing.JFrame {

    public TableModelElegirProductos tProductosPorTipo;
    public TableModelProductosPedidos tProductosAgregados;
    private final int bebida = 1;
    private final int comida = 2;
    public List<Productos> listaDeProductos;
    public Comanda comanda;
    public int rowElegida = 0;
    public boolean isSelected = Boolean.FALSE;
    Comandaproducto cp;
    public boolean status;

    /**
     * Creates new form ProductosView
     */
    public ProductosView() {
        initComponents();
    }

    public ProductosView(Comanda comanda, boolean status) {
        this.status = status;
        tProductosPorTipo = new TableModelElegirProductos();
        tProductosAgregados = new TableModelProductosPedidos();

        this.comanda = comanda;
        initComponents();
        jTMesa.setText(comanda.getIdmesa().getIdmesa().toString());
        jTMesero.setText(comanda.getIdusuario().getNombre()); 
        jTCuenta.setText(comanda.getIdcomanda().toString());
        tProductosAgregados.addComandaproducto(Main.comandaproductoFacade.findList(comanda.getIdcomanda()));
        
   }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBEliminar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBAlimentos = new javax.swing.JButton();
        jBBebidas = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTAgregados = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTCatalogo = new javax.swing.JTable();
        jBAgregar = new javax.swing.JButton();
        jBTerminar = new javax.swing.JButton();
        jTMesa = new javax.swing.JTextField();
        jTMesero = new javax.swing.JTextField();
        jTCuenta = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jBEliminar.setText("Eliminar Producto");
        jBEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarActionPerformed(evt);
            }
        });

        jLabel1.setText("Mesa:");

        jLabel2.setText("Mesero:");

        jLabel3.setText("Cuenta:");

        jBAlimentos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/burger-icon.png"))); // NOI18N
        jBAlimentos.setText("ALIMENTOS");
        jBAlimentos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAlimentosActionPerformed(evt);
            }
        });

        jBBebidas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/cafe.png"))); // NOI18N
        jBBebidas.setText("BEBIDAS");
        jBBebidas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBebidasActionPerformed(evt);
            }
        });

        jTAgregados.setModel(tProductosAgregados);
        jTAgregados.setColumnSelectionAllowed(true);
        jTAgregados.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTAgregados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTAgregadosMouseClicked(evt);
            }
        });
        jTAgregados.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTAgregadosKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(jTAgregados);

        jTCatalogo.setModel(tProductosPorTipo);
        jScrollPane2.setViewportView(jTCatalogo);

        jBAgregar.setText("Agregar Producto");
        jBAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAgregarActionPerformed(evt);
            }
        });

        jBTerminar.setText("Terminar");
        jBTerminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTerminarActionPerformed(evt);
            }
        });

        jTMesa.setEditable(false);
        jTMesa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTMesaActionPerformed(evt);
            }
        });

        jTMesero.setEditable(false);

        jTCuenta.setEditable(false);
        jTCuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCuentaActionPerformed(evt);
            }
        });

        jLabel4.setText("LISTA DE PRODUCTOS DE LA COMANDA");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(8, 8, 8)
                .addComponent(jTCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBAlimentos, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBBebidas, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jBEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBAgregar)
                        .addGap(31, 31, 31)))
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53))
            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTMesero, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTMesa, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(242, 242, 242)
                        .addComponent(jBTerminar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTMesa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBAlimentos)
                        .addComponent(jBBebidas))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTMesero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jBEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jBTerminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBAlimentosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAlimentosActionPerformed
        tProductosPorTipo.addCatalogoDeProductos(comida);
    }//GEN-LAST:event_jBAlimentosActionPerformed

    private void jBBebidasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBebidasActionPerformed
        tProductosPorTipo.addCatalogoDeProductos(bebida);
    }//GEN-LAST:event_jBBebidasActionPerformed

    private void jBAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAgregarActionPerformed
        int idProducto = (Integer) tProductosPorTipo.getValueAt(jTCatalogo.getSelectedRow(), 2);
        Productos producto = Main.productosFacade.find(idProducto);
        tProductosAgregados.addComandaproductoElegido(comanda, producto);
    }//GEN-LAST:event_jBAgregarActionPerformed

    private void jBTerminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTerminarActionPerformed

        Iterator iterator = tProductosAgregados.getDatalist().iterator();

        while (iterator.hasNext()) {
            Comandaproducto elemento = (Comandaproducto) iterator.next();
            System.out.println("Id comanda: " + elemento.getComanda().getIdcomanda());
            System.out.println("Id producto: " + elemento.getProductos().getIdproducto());
            System.out.println("cantidad: " + elemento.getCantidad());
            System.out.println("importe: " + elemento.getImporte());

            Main.comandaproductoFacade.edit(elemento);
        }
        JOptionPane.showMessageDialog(this, "LA COMANDA HA SIDO CREADA SE ENVIARAN LOS PEDIDOS A BARRA Y COCINA");
        
       this.dispose();
    }//GEN-LAST:event_jBTerminarActionPerformed

    private void jBEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarActionPerformed
        int idProducto = (Integer) tProductosPorTipo.getValueAt(jTCatalogo.getSelectedRow(), 2);
        Productos producto = Main.productosFacade.find(idProducto);
        
        tProductosAgregados.delComandaproductoElegido(comanda, producto);
        
       
       
    }//GEN-LAST:event_jBEliminarActionPerformed
    
    
    private void jTMesaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTMesaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTMesaActionPerformed

    private void jTCuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCuentaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTCuentaActionPerformed

    public void setRowElegida(int row) {
        this.rowElegida = row;

    }
    private void jTAgregadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTAgregadosMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTAgregadosMouseClicked

    private void jTAgregadosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTAgregadosKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTAgregadosKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProductosView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProductosView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProductosView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductosView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ProductosView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAgregar;
    private javax.swing.JButton jBAlimentos;
    private javax.swing.JButton jBBebidas;
    private javax.swing.JButton jBEliminar;
    private javax.swing.JButton jBTerminar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTAgregados;
    private javax.swing.JTable jTCatalogo;
    private javax.swing.JTextField jTCuenta;
    private javax.swing.JTextField jTMesa;
    private javax.swing.JTextField jTMesero;
    // End of variables declaration//GEN-END:variables
}
